#region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU General Public License.
/// </copyright>
#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Text;

namespace Osh.Configuration
{
	public interface IModeCollection : IConfigurationElementCollection
	{
		// Redefine IConfigurationElementCollection members to get access to them from the script.
		new int Count { get; }
		new IEnumerator GetEnumerator();

		bool TryGetModeByName(string modeName, out ModeElement modeElement);
		ModeElement[] GetModesByPlayerName(string playerName);
	}

	[ConfigurationCollection(typeof(ModeElement), AddItemName="mode",
		CollectionType=ConfigurationElementCollectionType.BasicMap)]
	[ClassInterface(ClassInterfaceType.None)]
	public class ModeCollection : ConfigurationElementCollection, IModeCollection
	{
		public ModeCollection()
		{
		}

		protected override System.Configuration.ConfigurationElement CreateNewElement()
		{
			return new ModeElement();
		}

		protected override object GetElementKey(System.Configuration.ConfigurationElement element)
		{
			return ((ModeElement)element).Name;
		}

		public void Add(ModeElement element)
		{
			BaseAdd(element);
		}

		public ModeElement GetMode(int index)
		{
			return (ModeElement)base.BaseGet(index);
		}

		public bool TryGetModeByName(string modeName, out ModeElement modeElement)
		{
			modeElement = (ModeElement)base.BaseGet(modeName);
			return modeElement != null;
		}

		public ModeElement[] GetModesByPlayerName(string playerName)
		{
			List<ModeElement> elements = new List<ModeElement>();
			foreach(ModeElement modeElement in this)
			{
				if(modeElement.Player == playerName)
				{
					elements.Add(modeElement);
				}
			}
			return elements.ToArray();
		}

		#region IConfigurationElementCollection Members
		public new int Count
		{
			get { return base.Count; }
		}

		public new IEnumerator GetEnumerator()
		{
			return base.GetEnumerator();
		}
		#endregion IConfigurationElementCollection Members
	}
}
